#!/usr/bin/env python3

def max_sum2(a, s):
    h = {}
    for i, v in enumerate(a):
        h[v] = i 
    for i, v in enumerate(a):
        try:
            return True, i, h[s-v]
        except:
            pass
    return False, 0, 0

def max_sum3(a, s):
    h = a[:]
    h.sort()
    i, j = 0, len(a) - 1 
    while True:
        v = h[i] + h[j]
        if v == s:
            return True, i, j 
        elif v > s:
            j -= 1
        else:
            i += 1
        if i == j:
            break 
    return False, 0, 0

            
    

if __name__ == '__main__':
    a = [2,4, 5, 7, 8, 20]
    s = 13
    print("origin : ", a)
    ret, p1, p2 = max_sum2(a, s)
    if ret:
        print("sum(%s) = a(%s) + b(%s)"%(s, a[p1], a[p2]))
    else:
        print("no sum")

    ret, p1, p2 = max_sum3(a, s)
    if ret:
        print("sum(%s) = a(%s) + b(%s)"%(s, a[p1], a[p2]))
    else:
        print("no sum")
